package graph2.weight;

/**
 * Created by yuh on 2018/4/3.
 */
public class Edge<Weight extends Number & Comparable> implements Comparable<Edge> {

    /**
     * 节点一
     */
    private int v;
    /**
     * 节点二
     */
    private int w;
    /**
     * 权重
     */
    private Weight weight;

    public Edge(int v, int w, Weight weight) {
        this.v = v;
        this.w = w;
        this.weight = weight;
    }

    public int v() {
        return v;
    }

    public int w() {
        return w;
    }

    public Weight weight(){
        return weight;
    }

    /**
     * 返回另一个节点
     *
     * @param v
     * @return
     */
    public int other(int v) {
        return this.v == v ? this.w : this.v;
    }

    /**
     * 根据权重比较
     * @param o
     * @return
     */
    @Override
    public int compareTo(Edge o) {
        if (this.weight.compareTo(o.weight) == 0) {
            return 0;
        } else if (this.weight.compareTo(o.weight) < 0) {
            return -1;
        } else {
            return 1;
        }
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[ from:").append(v).append(" to:").append(w).append(" weight:").append(weight).append("]");
        return stringBuilder.toString();
    }
}
